package com.ddone.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;

/**
 * @author ddone
 * @date 2024/3/27-08:39
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    @Bean
    public Docket docket() {
        ArrayList<Parameter> parameters = new ArrayList<>();
        Parameter parameter = new ParameterBuilder()
                .name("token")
                .description("用户token")
                .defaultValue("")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        parameters.add(parameter);
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("authManagerApi")
                .apiInfo(adminApiInfo())
                .select()
                // 只显示admin 路径下的页面
                .apis(RequestHandlerSelectors.basePackage("com.ddone"))
                .paths(PathSelectors.regex("/auth-admin/.*"))
                .build()
                .globalOperationParameters(parameters);

    }

    private ApiInfo adminApiInfo() {

        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了后台管理系统微服务接口定义")
                .version("1.0")
                .contact(new Contact("ddone", "http://ddone.com", "ddone@126.com"))
                .build();
    }

}
